package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.RoleMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
@Service
public class RoleSeviceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private PermissionService permissionService;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageData =null;
        //  条件构造器
        QueryWrapper<Role> wrapper = new QueryWrapper<Role>();
        wrapper.eq("is_delete",0);
        //  1  mp  分页查询   完成分页查询   select  *  from   xxx  limit     count(1)
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            //  无条件分页
            //  page  结合 分页插件
            pageData   = page(new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else{
            wrapper.eq("name",queryPageBean.getQueryString());
            pageData   = page(new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }
        // 2  结果集封装 PageResult返回
        PageResult pageResult = new PageResult(pageData.getTotal(),pageData.getRecords());
        return pageResult;
    }

    @Override
    public RoleVO findAllpermissionIdsAndPermission(Integer id) {
        RoleVO roleVO  = baseMapper.findRoleVO(id);
        List<Integer> permissionIdlist = baseMapper.fnndallpermissionIdlist(id);
        roleVO.setPermissionIds(permissionIdlist);
        List<Permission> list = permissionService.list();
        roleVO.setPermissionsList(list);
        return roleVO;
    }
    //todo
    @Override
    public Boolean addAndUpdate(RoleVO roleVO) {
        return null;
    }


}
